//Documentation complete
/**
 * @author Kazu
 * @version 2009MR20
 */

package ui.internal.ajax;

import control.*;
import problemDomain.*;
import static utilities.XMLTools.*;
import static problemDomain.ErrorBean.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.*;

/**
 * Servlet implementation class MaintainDelivery
 */
public class MaintainDelivery extends HttpServlet {
	
	/* Constants */
	
	private static final long serialVersionUID = 8188361443046900576L;
	
	/* END Constants */
	
	
	/* (HttpServlet) Methods */ 
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		response.setContentType("text/xml");
		if (session != null) {
			String action = request.getParameter("a");
			if (action != null) {
				if (action.equalsIgnoreCase("c"))
					create(request, response);
				else if (action.equalsIgnoreCase("r"))
					report(request, response);
				else if (action.equalsIgnoreCase("u"))
					update(request, response);
				else if (action.equalsIgnoreCase("d"))
					delete(request, response);
				else if (action.equalsIgnoreCase("l"))
					listAll(response);
				else if (action.equalsIgnoreCase("t"))
					listDwellingType(response);
				else if (action.equalsIgnoreCase("s"))
					listDeliveryStatus(response);
			} else
				response.setStatus(HttpServletResponse.SC_NO_CONTENT);
		} else
			response.setStatus(HttpServletResponse.SC_NO_CONTENT);
	}
	
	/* END (HttpServlet) Methods */ 
	
	
	/* Local Methods */
	
	private void create (HttpServletRequest request, HttpServletResponse response) throws IOException {
		String firstname = request.getParameter("firstname");
		String lastname = request.getParameter("lastname");
		String phone = request.getParameter("phone");
		String address = request.getParameter("address");
		String city = request.getParameter("city");
		String province = request.getParameter("province");
		String postalCode = request.getParameter("postal");
		String dwelling = request.getParameter("dwelling");
		String building = request.getParameter("building");
		String room = request.getParameter("room");
		String status = request.getParameter("status");
		String instruction = request.getParameter("instruction");
		String orderID = request.getParameter("orderid");
		
		PrintWriter out = response.getWriter();
		try {
			DeliveryControl delCtrl = new DeliveryControl();
			delCtrl.create(firstname, lastname, phone, address, city, province, postalCode, 
				dwelling, building, room, status, instruction, orderID);
			out.write(getSuccess().toXML());
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	private void report (HttpServletRequest request, HttpServletResponse response) throws IOException {
		String id = request.getParameter("id");
		String firstname = request.getParameter("firstname");
		String lastname = request.getParameter("lastname");
		String phone = request.getParameter("phone");
		
		PrintWriter out = response.getWriter();
		try {
			DeliveryControl delCtrl = new DeliveryControl();
			List<DeliveryBean> reportList = delCtrl.search(id, firstname, lastname, phone);
			out.write(encapsulateBeanListXML(reportList, "deliveries"));
		}
		catch (ErrorBean e) {
			out.println(e.toXML());
		}
	}
	
	private void update (HttpServletRequest request, HttpServletResponse response) throws IOException {
		String id = request.getParameter("id");
		String firstname = request.getParameter("firstname");
		String lastname = request.getParameter("lastname");
		String phone = request.getParameter("phone");
		String address = request.getParameter("address");
		String city = request.getParameter("city");
		String province = request.getParameter("province");
		String postalCode = request.getParameter("postal");
		String dwelling = request.getParameter("dwelling");
		String building = request.getParameter("building");
		String room = request.getParameter("room");
		String status = request.getParameter("status");
		String instruction = request.getParameter("instruction");
		
		PrintWriter out = response.getWriter();
		try {
			DeliveryControl delCtrl = new DeliveryControl();
			delCtrl.update(id, firstname, lastname, phone, address, city, province,
				postalCode, dwelling, building, room, status, instruction);
			out.write(getSuccess().toXML());
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	private void delete (HttpServletRequest request, HttpServletResponse response) throws IOException {
		String id = request.getParameter("id");
		PrintWriter out = response.getWriter();
		try {
			DeliveryControl delCtrl = new DeliveryControl();
			delCtrl.delete(id);
			out.println(getSuccess().toXML());
		} catch (ErrorBean e) {
			out.println(e.toXML());
		}
		
	}
	
	private void listAll (HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		try {
			DeliveryControl delCtrl = new DeliveryControl();
			List<DeliveryBean> list = delCtrl.listAll();
			out.write(encapsulateBeanListXML(list, "deliveries"));
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	private void listDwellingType(HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		try {
			DeliveryControl delCtrl = new DeliveryControl();
			List<String> list = delCtrl.listDwellingType();
			out.write(encapsulateStringListXML(list));
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}

	private void listDeliveryStatus (HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		response.setContentType("text/xml");
		try {
			DeliveryControl delCtrl = new DeliveryControl();
			List<String> list = delCtrl.listDeliveryStatus();
			out.write(encapsulateStringListXML(list));
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	/* END Local Methods */

}
